#include "userinfowidget.h"
#include <QGridLayout>
#include <QPainterPath>
#include <QPixmap>
#include <QPainter>

#include "model/datacenter.h"
#include "mainwidget.h"
using namespace model;

UserInfoWidget::UserInfoWidget(const UserInfo& userInfo, QWidget* parent) : QDialog(parent), userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setFixedSize(370, 180); // 调整窗口宽度，以容纳更宽的按钮布局
    this->setWindowTitle("用户详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->move(QCursor::pos());

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    // layout->setHorizontalSpacing(0);
    layout->setContentsMargins(10, 20, 10, 0); // 调整边距，左右对称，底部留更多空间
    layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    this->setLayout(layout);

    // 样式定义
    // Tag 标签样式
    QString labelStyle = "QLabel {"
                         "   font-size: 14px;"
                         "   font-weight: bold;"
                         "   color: #333333;"
                         "}";

    // Value 标签样式
    QString valueLabelStyle = "QLabel {"
                              "   font-size: 14px;"
                              "   color: #555555;"
                              "}";

    QString baseBtnStyle = "QPushButton {"
                           "   background-color: #E0E0E0;"
                           "   border: none;"
                           "   border-radius: 8px;"
                           "   color: #333333;"
                           "   font-size: 14px;"
                           "   padding: 8px 15px; "
                           "}"
                           "QPushButton:hover {"
                           "   background-color: #D0D0D0;"
                           "}"
                           "QPushButton:pressed {"
                           "   background-color: #C0C0C0;"
                           "}";

    QString primaryBtnStyle = "QPushButton {"
                              "   background-color: #4CAF50;"
                              "   border: none;"
                              "   border-radius: 8px;"
                              "   color: white;"
                              "   font-size: 14px;"
                              "   padding: 8px 15px;"
                              "}"
                              "QPushButton:hover {"
                              "   background-color: #45A049;"
                              "}"
                              "QPushButton:pressed {"
                              "   background-color: #39843C;"
                              "}";

    QString dangerBtnStyle = "QPushButton {"
                             "   background-color: #F44336;"
                             "   border: none;"
                             "   border-radius: 8px;"
                             "   color: white;"
                             "   font-size: 14px;"
                             "   padding: 8px 15px;"
                             "}"
                             "QPushButton:hover {"
                             "   background-color: #D32F2F;"
                             "}"
                             "QPushButton:pressed {"
                             "   background-color: #B71C1C;"
                             "}";

    int labelWidth = 40, labelHeight = 25;
    int btnWidth = 100, btnHeight = 35;

    // 3. 添加头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(80, 80);
    avatarBtn->setIconSize(QSize(80, 80));
    avatarBtn->setStyleSheet(R"(
        QPushButton {
            border: none;
            background-color: transparent;
        };
    )");

    QPixmap srcAvatar = userInfo.avatar.pixmap(80, 80);
    srcAvatar = model::getRoundRectPixmap(srcAvatar, QSize(80, 80), 8);
    avatarBtn->setIcon(QIcon(srcAvatar));

    // 4. 添加用户序号
    idTag = new QLabel();
    idTag->setText("序号: ");
    idTag->setStyleSheet(labelStyle);
    idTag->setFixedSize(labelWidth, labelHeight);
    idTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    idLabel = new QLabel();
    idLabel->setText(userInfo.userId);
    idLabel->setStyleSheet(valueLabelStyle);
    idLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 5. 添加用户昵称
    nameTag = new QLabel();
    nameTag->setText("昵称: ");
    nameTag->setStyleSheet(labelStyle);
    nameTag->setFixedSize(labelWidth, labelHeight);
    nameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    nameLabel = new QLabel();
    nameLabel->setText(userInfo.nickname);
    nameLabel->setStyleSheet(valueLabelStyle);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 6. 设置电话
    phoneTag = new QLabel();
    phoneTag->setText("电话: ");
    phoneTag->setStyleSheet(labelStyle);
    phoneTag->setFixedSize(labelWidth, labelHeight);
    phoneTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    phoneLabel = new QLabel();
    phoneLabel->setText(userInfo.phone);
    phoneLabel->setStyleSheet(valueLabelStyle);
    phoneLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 7. 添加功能按钮
    applyBtn = new QPushButton();
    applyBtn->setText("申请好友");
    applyBtn->setFixedSize(btnWidth, btnHeight);
    applyBtn->setStyleSheet(baseBtnStyle);
    applyBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    sendMessageBtn = new QPushButton();
    sendMessageBtn->setText("发送消息");
    sendMessageBtn->setFixedSize(btnWidth, btnHeight);
    sendMessageBtn->setStyleSheet(primaryBtnStyle);
    sendMessageBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setFixedSize(btnWidth, btnHeight);
    deleteFriendBtn->setStyleSheet(dangerBtnStyle);
    deleteFriendBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 8. 添加上述内容到布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 3, 1, Qt::AlignVCenter | Qt::AlignHCenter); // 头像垂直居中，水平居中

    // id
    QWidget* idWidget = new QWidget();
    QHBoxLayout* idLayout = new QHBoxLayout(idWidget);
    idLayout->setContentsMargins(15, 0, 0, 0);
    idLayout->setSpacing(0); // 调整这个值来控制间距
    idLayout->addWidget(idTag);
    idLayout->addWidget(idLabel);
    layout->addWidget(idWidget, 0, 1, Qt::AlignVCenter); // 注意：这里只占一列

    // 昵称
    QWidget* nameWidget = new QWidget();
    QHBoxLayout* nameLayout = new QHBoxLayout(nameWidget);
    nameLayout->setContentsMargins(15, 0, 0, 0);
    nameLayout->setSpacing(0);
    nameLayout->addWidget(nameTag);
    nameLayout->addWidget(nameLabel);
    layout->addWidget(nameWidget, 1, 1, Qt::AlignVCenter | Qt::AlignLeft);

    // 电话
    QWidget* phoneWidget = new QWidget();
    QHBoxLayout* phoneLayout = new QHBoxLayout(phoneWidget);
    phoneLayout->setContentsMargins(15, 0, 0, 0);
    phoneLayout->setSpacing(0);
    phoneLayout->addWidget(phoneTag);
    phoneLayout->addWidget(phoneLabel);
    layout->addWidget(phoneWidget, 2, 1, Qt::AlignVCenter | Qt::AlignLeft);


    // 按钮
    // // 优化：使用 QHBoxLayout 实现按钮均匀居中分布
    // QHBoxLayout* buttonLayout = new QHBoxLayout();
    // buttonLayout->addStretch(); // 左侧弹簧，将按钮整体推向中间
    // buttonLayout->addWidget(applyBtn);
    // buttonLayout->addSpacing(20); // 按钮之间的固定间距
    // buttonLayout->addWidget(sendMessageBtn);
    // buttonLayout->addSpacing(20); // 按钮之间的固定间距
    // buttonLayout->addWidget(deleteFriendBtn);
    // buttonLayout->addStretch(); // 右侧弹簧，将按钮整体推向中间
    // // buttonLayout->setSpacing(20); // 可以在这里设置统一间距，或者用addSpacing更灵活
    // buttonLayout->setContentsMargins(0, 0, 0, 0); // 移除按钮布局的内边距

    // // 将按钮布局添加到主网格布局的最后一行，跨越所有列
    // layout->addLayout(buttonLayout, 3, 0, 1, 3); // 第3行，从第0列开始，跨1行3列

    layout->addWidget(applyBtn, 3, 0, Qt::AlignLeft);
    layout->addWidget(sendMessageBtn, 3, 1, Qt::AlignCenter);
    layout->addWidget(deleteFriendBtn, 3, 2, Qt::AlignRight);
    layout->setRowMinimumHeight(3, btnWidth);

    // 9. 设置按钮的禁用状态
    DataCenter* dataCenter = DataCenter::getInstance();
    auto* myFriend = dataCenter->findFriendById(this->userInfo.userId);
    QString btnStyle = R"(
        QPushButton {
           background-color: rgb(225, 225, 225);
           color: rgb(155, 155, 155);
           border: none;
           border-radius: 8px;
           font-size: 14px;
           padding: 8px 15px;
        }
    )";
    if(myFriend == nullptr){
        applyBtn->setEnabled(true);
        applyBtn->setStyleSheet(primaryBtnStyle);
        sendMessageBtn->setEnabled(false);
        sendMessageBtn->setStyleSheet(btnStyle);
        deleteFriendBtn->setEnabled(false);
        deleteFriendBtn->setStyleSheet(btnStyle);
    }else{
        applyBtn->setEnabled(false);
        applyBtn->setStyleSheet(btnStyle);
        sendMessageBtn->setEnabled(true);
        sendMessageBtn->setStyleSheet(primaryBtnStyle);
        deleteFriendBtn->setEnabled(true);
        deleteFriendBtn->setStatusTip(dangerBtnStyle);
    }

    // 10. 初始化信号槽
    initSignalSlot();
}

void UserInfoWidget::initSignalSlot(){
    connect(sendMessageBtn, &QPushButton::clicked, this, [=]() {
        // 拿到主窗口指针, 通过主窗口中, 前面实现的 切换到会话 这样的功能, 直接调用即可.
        MainWidget* mainWidget = MainWidget::getInstance();
        mainWidget->switchSession(userInfo.userId);

        // 把本窗口关闭掉
        this->close();
    });

    connect(deleteFriendBtn, &QPushButton::clicked, this, &UserInfoWidget::clickDeleteFriendBtn);
    connect(applyBtn, &QPushButton::clicked, this, &UserInfoWidget::clickApplyBtn);
}

void UserInfoWidget::clickDeleteFriendBtn(){
    // 1. 弹出对话框确认是否删除
    auto result = QMessageBox::warning(this, "确认删除", "确认删除当前好友?", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok) {
        LOG() << "删除好友取消";
        return;
    }
    // 2. 发送请求删除
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->deleteFriendAsync(userInfo.userId);
    this->close();
}

void UserInfoWidget::clickApplyBtn(){
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->addFriendApplyAsync(userInfo.userId);
    this->close();
}







































